﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using System.Drawing;

namespace Jemgine.MapEditor
{
    public class BaseBoneTool : Tool
    {
        Data.Skeleton SelectedSkeleton = null;

        public override void BeforeDraw(EditorState EditorState)
        {
            SelectedSkeleton = EditorState.GetMiddleObject(EditorState.Selected) as Data.Skeleton;
            if (SelectedSkeleton != null)
                SelectedSkeleton.SetTransform(SelectedSkeleton.GetFrameAngles(SelectedSkeleton.EditorFrame));
        }

        public override void AfterDraw(EditorState EditorState)
        {
            if (SelectedSkeleton != null) SelectedSkeleton.SetIdentityTransform();
        }

        protected void SelectBone(Vector2 MousePosition, Data.Skeleton SelectedSkeleton, EditorState EditorState)
        {
            SelectedSkeleton.SetTransform(SelectedSkeleton.GetFrameAngles(SelectedSkeleton.EditorFrame));

            var InteriorPosition = SelectedSkeleton.Owner.TransformToLocalSpace(MousePosition);
            for (int BID = 0; BID < SelectedSkeleton.Bones.Count; ++BID)
            {
                var BonePosition = Vector2.Transform(SelectedSkeleton.Bones[BID].Position, 
                    SelectedSkeleton.Bones[BID].Transform);
                if (EditorState.PointInItem(Vector2.One, BonePosition, InteriorPosition))
                {
                    var Selection = new Engine.Selection(Engine.Selection.Types.Vertex,
                        EditorState.Selected.TopIndex, EditorState.Selected.MiddleIndex,
                        BID);
                    EditorState.SelectItem(Selection);
                }
            }

            SelectedSkeleton.SetIdentityTransform();
        }

    }
}
